package eleven2Twenty;

import java.util.Date;

/**
 * 输入一个整数，带打印1到最大的n位数
 * Created by Wongy on 2018/3/14.
 */
public class Twelve12 {
    //第一种方法
    private static void solution1(int n) {
        assert n >= 0;
        char[] chars = new char[n];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = '0';
        }
        while (!incrementNum(chars)) {
            printNum(chars);
        }
    }

    private static boolean incrementNum(char[] chars) {
        boolean isOverFlow = false;
        int length = chars.length;
        int offset = 0;
        for (int i = length - 1; i >= 0; i--) {
            int num = chars[i] - '0' + offset;
            if (i == length - 1) {
                num++;
            }
            if (num >= 10) {
                if (i == 0) {
                    isOverFlow = true;
                } else {
                    num -= 10;
                    offset = 1;
                    chars[i] = (char) ('0' + num);
                }
            } else {
                chars[i] = (char) ('0' + num);
                break;
            }
        }
        return isOverFlow;
    }

    private static void printNum(char[] chars) {
        boolean isBeginning0 = true;
        int length = chars.length;
        for (char aChar : chars) {
            if (isBeginning0 && aChar != '0') {
                isBeginning0 = false;
            }
            if (!isBeginning0) {
                System.out.print(aChar);
            }
        }
        System.out.println();
    }

    // // ====================方法二:递归====================
    public static void Print1ToMaxOfNDigits(int n) {
        if (n <= 0)
            return;

        char[] number = new char[n];
        Print1ToMaxOfNDigitsRecursively(number, n, 0);
//        for (int i = 0; i < 10; ++i) {
//            number[0] = (char) (i + '0');
//            Print1ToMaxOfNDigitsRecursively(number, n, 0);
//        }

    }

    private static void Print1ToMaxOfNDigitsRecursively(char[] number, int length, int index) {
        if (index == length) {
            printNum(number);
            return;
        }

        for (int i = 0; i < 10; i++) {
            number[index] = (char) (i + '0');
            Print1ToMaxOfNDigitsRecursively(number, length, index + 1);
        }
    }

    public static void main(String[] args) {
//        solution1(14);
//        Print1ToMaxOfNDigits(5);
        solution2(-1);
    }

    private static char[] chars;
    private static int length;

    //----自己写的递归
    private static void solution2(int n) {
        if (n   <= 0) return;
        chars = new char[n];
        length = n;
        name(0);
    }

    private static void name(int index) {
        if (index == length) {
            printNum(chars);
            return;
        }
        for (int i = 0; i < 10; i++) {
            chars[index] = (char) (i + '0');
            name(index + 1);
        }
    }
}
